<?php
/**
 * Created by PhpStorm.
 * User: LiuPeng
 * Date: 2018/4/4
 * Time: 14:38
 */

class ddk {
    const API_ROOT = 'http://gw-api.pinduoduo.com/api/router';
    const GOODS_DETAIL = 'pdd.ddk.goods.detail';
    const GOODS_SEARCH = 'pdd.ddk.goods.search';
    const GOODS_PID_QUERY = 'pdd.ddk.goods.pid.query';
    const GOODS_PID_GENERATE = 'pdd.ddk.goods.pid.generate';
    const GOODS_PROMOTION_URL_GENERATE = 'pdd.ddk.goods.promotion.url.generate';
    const ORDER_LIST_GET = 'pdd.ddk.order.list.get';
    const ORDER_LIST_RANGE_GET = 'pdd.ddk.order.list.range.get';

    ///////////////////////// 白名单用户 BEGIN /////////////////////////
    const ACT_PROM_URL_GENERATE = 'pdd.ddk.act.prom.url.generate'; // 生成分享红包赚佣金活动推广链接
    const RP_PROM_URL_GENERATE = 'pdd.ddk.rp.prom.url.generate'; // 生成红包推广链接接口

    const CHECK_IN_PROM_BILL_INCR_GET = 'pdd.ddk.check.in.prom.bill.incr.get'; // 签到红包分享账单列表（需要日均订单5000以上才有调用此接口权限）
    const CHECK_IN_PROM_URL_GENERATE = 'pdd.ddk.check.in.prom.url.generate'; // 生成签到分享推广链接（需要日均订单5000以上才有调用此接口权限）
    ///////////////////////// 白名单用户 END /////////////////////////

    const ORDER_LIST_INCREMENT_GET = 'pdd.ddk.order.list.increment.get'; // 增量查询推广订单信息（根据最后更新时间）

    const CMS_PROM_URL_GENERATE = 'pdd.ddk.cms.prom.url.generate'; // 生成商城推广链接接口
    const THEME_LIST_GET = 'pdd.ddk.theme.list.get'; // 查询多多进宝主题列表
    const THEME_GOODS_SEARCH = 'pdd.ddk.theme.goods.search'; // 多多进宝主题商品查询
    const THEME_PROM_URL_GENERATE = 'pdd.ddk.theme.prom.url.generate'; // 多多进宝主题活动推广链接生成

    const APP_NEW_BILL_LIST_GET = 'pdd.ddk.app.new.bill.list.get'; // 获取并查看多多客拉新的奖励账单

    const GOODS_CATS_GET = 'pdd.goods.cats.get';

    const URL_GET_CODE = 'http://mms.pinduoduo.com/open.html?';
    const URL_GET_ACCESS_TOKEN = 'http://open-api.pinduoduo.com/oauth/token';

    private $client_id = 'cb89f77f441c480691b4d30cdd428cde';
    private $client_secret = 'ce0bbfa4d9d8b13151e91b84029e3d61d94ed3f7';

    const CALLBACK = 'http://wxb.whwangdoudou.cn/pdd.php';

    public function __construct($options = array()) {
        if (isset($options['client_id'])) {
            $this->client_id = $options['client_id'];
        }
        if (isset($options['client_secret'])) {
            $this->client_secret = $options['client_secret'];
        }
    }

    public function get_access_token() {
        $ddk = DB::fetch_first("SELECT * FROM ".DB::table('pdd_auth')." WHERE client_id ='" . $this->client_id . "' AND client_secret = '" . $this->client_secret . "'");
        if (!empty($ddk) && $ddk['expires_in'] > TIMESTAMP) {
            return $ddk['access_token'];
        }

        return false;
    }

    public function set_access_token($arr) {
        $ddk = DB::fetch_first("SELECT * FROM ".DB::table('pdd_auth')." WHERE client_id ='" . $this->client_id . "' AND client_secret = '" . $this->client_secret . "'");
        if (empty($ddk)) {
            DB::insert('pdd_auth',
                array(
                    'client_id' => $this->client_id,
                    'client_secret' => $this->client_secret,
                    'access_token' => $arr['access_token'],
                    'expires_in' => $arr['expires_in'] + TIMESTAMP,
                    'refresh_token' => $arr['refresh_token'],
                    'scope' => implode(',', $arr['scope']),
                    'owner_id' => $arr['owner_id'],
                    'owner_name' => $arr['owner_name'],
                    'dateline' => TIMESTAMP
                ),
                1);
        } else {
            DB::update('pdd_auth',
                array(
                    'access_token' => $arr['access_token'],
                    'expires_in' => $arr['expires_in'] + TIMESTAMP,
                    'refresh_token' => $arr['refresh_token'],
                    'scope' => implode(',', $arr['scope']),
                    'owner_id' => $arr['owner_id'],
                    'owner_name' => $arr['owner_name'],
                    'dateline' => TIMESTAMP
                ),
                "client_id='".$this->client_id."' AND client_secret='".$this->client_secret."'");
        }
    }

    public function refresh_access_token() {
        $check = DB::fetch_first("SELECT * FROM ".DB::table('pdd_auth')." WHERE client_id ='" . $this->client_id . "' AND client_secret = '" . $this->client_secret . "'");
        if (empty($check)) {
            return false;
        } else {
            if ($check['expires_in'] > TIMESTAMP) {
                return $check['access_token'];
            } else {
                $refresh_token = $check['refresh_token'];
                $result = $this->getOauthRefreshToken($refresh_token);

                if ($result) {
                    $this->set_access_token($result);
                    return $result['access_token'];
                }
            }
        }

        return false;
    }

    public function getOauthRedirect($callback = '') {
        if (empty($callback)) $callback = self::CALLBACK;
        return self::URL_GET_CODE.'response_type=code&client_id='.$this->client_id.'&redirect_uri='.urlencode($callback);
    }

    public function getOauthAccessToken($callback = '') {
        if (empty($callback)) $callback = self::CALLBACK;
        $code = isset($_GET['code'])?$_GET['code']:'';
        if (!$code) return false;
        $result = $this->post(self::URL_GET_ACCESS_TOKEN,
            json_encode(array(
                'client_id' => $this->client_id,
                'client_secret' => $this->client_secret,
                'grant_type' => 'authorization_code',
                'code' => $code,
                'redirect_uri' => $callback
            )),
            true
        );
        if ($result) {
            $arr = json_decode($result, true);

            return $arr;
        }

        return false;
    }

    public function getOauthRefreshToken($refresh_token) {
        $result = $this->post(self::URL_GET_ACCESS_TOKEN,
            json_encode(array(
                'client_id' => $this->client_id,
                'client_secret' => $this->client_secret,
                'grant_type' => 'refresh_token',
                'refresh_token' => $refresh_token
            )),
            true
        );

        if ($result) {
            $arr = json_decode($result, true);

            return $arr;
        }

        return false;
    }

    private function post($url, $param, $is_get_access_token = false) {
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
        }

        if ($is_get_access_token) {
            $headers = array("Content-Type: application/json");
        } else {
            $headers = array("Content-type: multipart/form-data;charset=utf-8");
        }

        /*if (is_string($param)) {
            $strPOST = $param;
        } else if (is_array($param)) {
            $strPOST = json_encode($param);
        }*/
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $param);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);

        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }

    private function getSignature($param) {
        // 签名步骤一：按字典序排序参数
        ksort($param);
        // 签名步骤二：连接字符串，并在首尾加上client_secret
        $string = '';
        $string .= $this->client_secret;
        foreach ($param as $k => $v) {
            $string .= $k.$v;
        }
        $string .= $this->client_secret;
        //签名步骤三：MD5加密
        $string = MD5($string);
        //签名步骤四：所有字符转为大写
        $sign = strtoupper($string);
        return $sign;
    }

    public function goods_detail($goods_id) {
        if (empty($goods_id)) return false;

        $param = array(
            'type' => self::GOODS_DETAIL,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON',
            'goods_id_list' => '['.$goods_id.']'
        );

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
              "goods_detail_response": {
                "goods_details": [
                  {
                    "goods_id": 812681665,
                    "goods_name": "达利园豆本豆非转基因纯豆奶早餐奶蛋白饮料12*250ML",
                    "goods_desc": "达利集团豆本豆 原味豆奶250ml*12 低饱和脂肪早餐奶植物蛋白饮料,收到货如有破损，请正常签收，破损几个联系客服退几盒的差价，介意勿拍，谢谢大家体谅，\n",
                    "goods_image_url": "http://t08img.yangkeduo.com/images/2018-03-31/dcf0c57e16b65f4596a684045f6f6f5f.jpeg",
                    "goods_gallery_urls": [
                      "http://t09img.yangkeduo.com/images/2018-03-27/fc2a43e861f3fe4c2561740af283b3fb.jpeg",
                      "http://t10img.yangkeduo.com/images/2018-03-25/6042b3c5e9da71d21f84372995e10365.jpeg",
                      "http://t05img.yangkeduo.com/images/2018-03-31/3ac44908c6a66afa5cf995ad553f1f67.jpeg"
                    ],
                    "sold_quantity": 6823,
                    "min_group_price": 2990,
                    "min_normal_price": 4990,
                    "mall_name": "恒东食品专营店",
                    "category_id": 1,
                    "category_name": "美食",
                    "has_coupon": true,
                    "coupon_min_order_amount": 800,
                    "coupon_discount": 800,
                    "coupon_total_quantity": 35000,
                    "coupon_remain_quantity": 13890,
                    "coupon_start_time": 1521993600,
                    "coupon_end_time": 1524499199,
                    "promotion_rate": 200,
                    "goods_eval_score": 0,
                    "goods_eval_count": 0
                  }
                ]
              }
            }
        */
    }

    public function goods_search($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::GOODS_SEARCH,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['keyword'])) $param['keyword'] = $option['keyword'];
        if (isset($option['opt_id'])) $param['opt_id'] = $option['opt_id'];
        if (isset($option['page'])) $param['page'] = $option['page'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['sort_type'])) $param['sort_type'] = $option['sort_type']; // 排序方式：0-综合排序；1-按佣金比率升序；2-按佣金比例降序；3-按价格升序；4-按价格降序；5-按销量升序；6-按销量降序；7-优惠券金额排序升序；8-优惠券金额排序降序；9-券后价升序排序；10-券后价降序排序；11-按照加入多多进宝时间升序；12-按照加入多多进宝时间降序
        if (isset($option['with_coupon'])) $param['with_coupon'] = $option['with_coupon'];
        if (isset($option['range_list'])
            && is_array($option['range_list'])
            && !empty($option['range_list'])) {
            $param['range_list'] = json_encode($option['range_list']);
        }
        if (isset($option['cat_id'])) $param['cat_id'] = $option['cat_id'];
        if (isset($option['goods_id_list'])) { // [123456,123]
            if (is_string($option['goods_id_list'])) {
                $param['goods_id_list'] = $option['goods_id_list'];
            } else if (is_array($option['goods_id_list'])) {
                $param['goods_id_list'] = '['.implode(',', $option['goods_id_list']).']';
            }
        }

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
              "goods_search_response": {
                "goods_list": [
                  {
                    "goods_id": 595564668,
                    "goods_name": "4条装/男士内裤高档纯棉弹力棉中腰平角内裤宽松大码吸湿透气内裤男短裤底裤裤头无痕内裤青年纯棉裆四角裤衩潮平角裤",
                    "goods_desc": null,
                    "goods_thumbnail_url": "http://t04img.yangkeduo.com/images/2018-03-01/1ca25ffc5970a992d0e13d0f298280ee.jpeg",
                    "goods_image_url": "http://t05img.yangkeduo.com/images/2018-03-01/2948f580ade557320c7e9267f497940b.jpeg",
                    "goods_gallery_urls": null,
                    "sold_quantity": 67,
                    "min_group_price": 3890,
                    "min_normal_price": 4400,
                    "mall_name": "雅姿网商",
                    "category_id": 743,
                    "category_name": "男装",
                    "has_coupon": true,
                    "coupon_min_order_amount": 3700,
                    "coupon_discount": 2000,
                    "coupon_total_quantity": 5000,
                    "coupon_remain_quantity": 3800,
                    "coupon_start_time": 1520870400,
                    "coupon_end_time": 1521475199,
                    "promotion_rate": 300,
                    "goods_eval_score": 0,
                    "goods_eval_count": 0
                  }
                ],
                "total_count": 24
              }
            }
        */
    }

    private function goods_cats_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::GOODS_CATS_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['parent_cat_id'])) $param['parent_cat_id'] = $option['parent_cat_id'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
                "goods_cats_get_response": {
                    "goods_cats_list": [{
                        "level": 1,
                        "cat_id": 2,
                        "parent_cat_id": 0,
                        "cat_name": "食品饮料"
                    }, {
                        "level": 1,
                        "cat_id": 69,
                        "parent_cat_id": 0,
                        "cat_name": "美容护肤/精油"
                    }, {
                        "level": 1,
                        "cat_id": 77,
                        "parent_cat_id": 0,
                        "cat_name": "母婴玩具"
                    }, {
                        "level": 1,
                        "cat_id": 80,
                        "parent_cat_id": 0,
                        "cat_name": "虚拟商品"
                    }, {
                        "level": 1,
                        "cat_id": 83,
                        "parent_cat_id": 0,
                        "cat_name": "水果生鲜"
                    }, {
                        "level": 1,
                        "cat_id": 210,
                        "parent_cat_id": 0,
                        "cat_name": "女装"
                    }, {
                        "level": 1,
                        "cat_id": 239,
                        "parent_cat_id": 0,
                        "cat_name": "男装"
                    }, {
                        "level": 1,
                        "cat_id": 259,
                        "parent_cat_id": 0,
                        "cat_name": "饰品首饰"
                    }, {
                        "level": 1,
                        "cat_id": 263,
                        "parent_cat_id": 0,
                        "cat_name": "女鞋"
                    }, {
                        "level": 1,
                        "cat_id": 270,
                        "parent_cat_id": 0,
                        "cat_name": "男鞋"
                    }, {
                        "level": 1,
                        "cat_id": 277,
                        "parent_cat_id": 0,
                        "cat_name": "箱包"
                    }, {
                        "level": 1,
                        "cat_id": 281,
                        "parent_cat_id": 0,
                        "cat_name": "健身"
                    }, {
                        "level": 1,
                        "cat_id": 282,
                        "parent_cat_id": 0,
                        "cat_name": "厨房用品"
                    }, {
                        "level": 1,
                        "cat_id": 479,
                        "parent_cat_id": 0,
                        "cat_name": "家庭清洁"
                    }, {
                        "level": 1,
                        "cat_id": 483,
                        "parent_cat_id": 0,
                        "cat_name": "洗护纸品"
                    }, {
                        "level": 1,
                        "cat_id": 489,
                        "parent_cat_id": 0,
                        "cat_name": "居家日用"
                    }, {
                        "level": 1,
                        "cat_id": 519,
                        "parent_cat_id": 0,
                        "cat_name": "收纳整理"
                    }, {
                        "level": 1,
                        "cat_id": 525,
                        "parent_cat_id": 0,
                        "cat_name": "宠物用品"
                    }, {
                        "level": 1,
                        "cat_id": 873,
                        "parent_cat_id": 0,
                        "cat_name": "床上用品"
                    }, {
                        "level": 1,
                        "cat_id": 879,
                        "parent_cat_id": 0,
                        "cat_name": "布艺家饰"
                    }, {
                        "level": 1,
                        "cat_id": 909,
                        "parent_cat_id": 0,
                        "cat_name": "汽车用品"
                    }, {
                        "level": 1,
                        "cat_id": 990,
                        "parent_cat_id": 0,
                        "cat_name": "腕表眼镜"
                    }, {
                        "level": 1,
                        "cat_id": 1166,
                        "parent_cat_id": 0,
                        "cat_name": "珠宝黄金"
                    }, {
                        "level": 1,
                        "cat_id": 1366,
                        "parent_cat_id": 0,
                        "cat_name": "服饰配件"
                    }, {
                        "level": 1,
                        "cat_id": 1432,
                        "parent_cat_id": 0,
                        "cat_name": "个护美体"
                    }, {
                        "level": 1,
                        "cat_id": 1464,
                        "parent_cat_id": 0,
                        "cat_name": "彩妆/香水/美妆工具"
                    }, {
                        "level": 1,
                        "cat_id": 1629,
                        "parent_cat_id": 0,
                        "cat_name": "家居饰品"
                    }, {
                        "level": 1,
                        "cat_id": 1715,
                        "parent_cat_id": 0,
                        "cat_name": "成人用品"
                    }, {
                        "level": 1,
                        "cat_id": 1889,
                        "parent_cat_id": 0,
                        "cat_name": "鲜花园艺"
                    }, {
                        "level": 1,
                        "cat_id": 1896,
                        "parent_cat_id": 0,
                        "cat_name": "住宅家具"
                    }, {
                        "level": 1,
                        "cat_id": 2351,
                        "parent_cat_id": 0,
                        "cat_name": "运动"
                    }, {
                        "level": 1,
                        "cat_id": 2479,
                        "parent_cat_id": 0,
                        "cat_name": "家居建材"
                    }, {
                        "level": 1,
                        "cat_id": 2603,
                        "parent_cat_id": 0,
                        "cat_name": "办公用品"
                    }, {
                        "level": 1,
                        "cat_id": 2629,
                        "parent_cat_id": 0,
                        "cat_name": "文化用品"
                    }, {
                        "level": 1,
                        "cat_id": 2933,
                        "parent_cat_id": 0,
                        "cat_name": "3C数码配件"
                    }, {
                        "level": 1,
                        "cat_id": 3202,
                        "parent_cat_id": 0,
                        "cat_name": "书籍杂志"
                    }, {
                        "level": 1,
                        "cat_id": 3817,
                        "parent_cat_id": 0,
                        "cat_name": "内衣裤袜"
                    }, {
                        "level": 1,
                        "cat_id": 4173,
                        "parent_cat_id": 0,
                        "cat_name": "户外"
                    }, {
                        "level": 1,
                        "cat_id": 4886,
                        "parent_cat_id": 0,
                        "cat_name": "全屋定制"
                    }, {
                        "level": 1,
                        "cat_id": 4958,
                        "parent_cat_id": 0,
                        "cat_name": "乐器/吉他/钢琴/配件"
                    }, {
                        "level": 1,
                        "cat_id": 5126,
                        "parent_cat_id": 0,
                        "cat_name": "模玩/动漫/周边/cos/桌游"
                    }, {
                        "level": 1,
                        "cat_id": 5217,
                        "parent_cat_id": 0,
                        "cat_name": "古董/邮币/字画/收藏"
                    }, {
                        "level": 1,
                        "cat_id": 5752,
                        "parent_cat_id": 0,
                        "cat_name": "影音电器"
                    }, {
                        "level": 1,
                        "cat_id": 5834,
                        "parent_cat_id": 0,
                        "cat_name": "手机"
                    }, {
                        "level": 1,
                        "cat_id": 5839,
                        "parent_cat_id": 0,
                        "cat_name": "二手数码"
                    }, {
                        "level": 1,
                        "cat_id": 5851,
                        "parent_cat_id": 0,
                        "cat_name": "电脑"
                    }, {
                        "level": 1,
                        "cat_id": 5906,
                        "parent_cat_id": 0,
                        "cat_name": "数码相机/单反相机/摄像机"
                    }, {
                        "level": 1,
                        "cat_id": 5921,
                        "parent_cat_id": 0,
                        "cat_name": "智能设备"
                    }, {
                        "level": 1,
                        "cat_id": 5955,
                        "parent_cat_id": 0,
                        "cat_name": "厨房电器"
                    }, {
                        "level": 1,
                        "cat_id": 6076,
                        "parent_cat_id": 0,
                        "cat_name": "大家电"
                    }, {
                        "level": 1,
                        "cat_id": 6128,
                        "parent_cat_id": 0,
                        "cat_name": "生活电器"
                    }, {
                        "level": 1,
                        "cat_id": 6209,
                        "parent_cat_id": 0,
                        "cat_name": "个人护理保健"
                    }, {
                        "level": 1,
                        "cat_id": 6290,
                        "parent_cat_id": 0,
                        "cat_name": "网络设备"
                    }, {
                        "level": 1,
                        "cat_id": 6398,
                        "parent_cat_id": 0,
                        "cat_name": "零食/坚果/特产"
                    }, {
                        "level": 1,
                        "cat_id": 6536,
                        "parent_cat_id": 0,
                        "cat_name": "咖啡/麦片/冲饮"
                    }, {
                        "level": 1,
                        "cat_id": 6586,
                        "parent_cat_id": 0,
                        "cat_name": "茶"
                    }, {
                        "level": 1,
                        "cat_id": 6630,
                        "parent_cat_id": 0,
                        "cat_name": "粮油米面/南北干货/调味品"
                    }, {
                        "level": 1,
                        "cat_id": 6758,
                        "parent_cat_id": 0,
                        "cat_name": "酒类"
                    }, {
                        "level": 1,
                        "cat_id": 6785,
                        "parent_cat_id": 0,
                        "cat_name": "保健食品/膳食营养补充食品"
                    }, {
                        "level": 1,
                        "cat_id": 6883,
                        "parent_cat_id": 0,
                        "cat_name": "传统滋补品"
                    }, {
                        "level": 1,
                        "cat_id": 7323,
                        "parent_cat_id": 0,
                        "cat_name": "电子元器件市场"
                    }, {
                        "level": 1,
                        "cat_id": 7629,
                        "parent_cat_id": 0,
                        "cat_name": "摩托车/装备/配件"
                    }, {
                        "level": 1,
                        "cat_id": 7639,
                        "parent_cat_id": 0,
                        "cat_name": "汽车/用品/配件/改装"
                    }, {
                        "level": 1,
                        "cat_id": 8172,
                        "parent_cat_id": 0,
                        "cat_name": "水产肉类/新鲜蔬果/熟食"
                    }]
                }
            }
        */
    }

    public function get_goods_cats($option) {
        $result = $this->goods_cats_get($option);
        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr) && array_key_exists('goods_cats_get_response', $arr)) {
                if (array_key_exists('goods_cats_list', $arr['goods_cats_get_response'])) {
                    return $arr['goods_cats_get_response']['goods_cats_list'];
                }
            }
        }

        return false;
    }

    public function goods_pid_query($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::GOODS_PID_QUERY,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['page'])) $param['page'] = $option['page'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
              "p_id_query_response": {
                "p_id_list": [
                  {
                    "p_id": "81_1812886",
                    "create_time": 1517724155
                  },
                  {
                    "p_id": "81_1812888",
                    "create_time": 1517916590
                  },
                  {
                    "p_id": "81_1812889",
                    "create_time": 1517916590
                  },
                  {
                    "p_id": "81_1812890",
                    "create_time": 1517916634
                  },
                  {
                    "p_id": "81_1812891",
                    "create_time": 1517916634
                  },
                  {
                    "p_id": "81_1812892",
                    "create_time": 1517917263
                  },
                  {
                    "p_id": "81_1812893",
                    "create_time": 1517917263
                  },
                  {
                    "p_id": "81_1812894",
                    "create_time": 1517917855
                  },
                  {
                    "p_id": "81_1812895",
                    "create_time": 1517917855
                  },
                  {
                    "p_id": "81_1812896",
                    "create_time": 1517917862
                  }
                ],
                "total_count": 17
              }
            }
        */
    }

    public function goods_pid_generate($number = 1) {
        if (empty($number)) return false;
        $param = array(
            'type' => self::GOODS_PID_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON',
            'number' => $number
        );

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
              "p_id_generate_response": {
                "p_id_list": [
                  {
                    "p_id": "81_1812902"
                  },
                  {
                    "p_id": "81_1812903"
                  }
                ]
              }
            }
        */
    }

    public function get_p_id() {
        $result = $this->goods_pid_generate();
        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr) && array_key_exists('p_id_generate_response', $arr)) {
                if (array_key_exists('p_id_list', $arr['p_id_generate_response'])) {
                    $p_id_list = $arr['p_id_generate_response']['p_id_list'];
                    if (!empty($p_id_list) && array_key_exists('p_id', $p_id_list[0])) {
                        return $p_id_list[0]['p_id'];
                    }
                }
            }
        }

        return false;
    }

    public function get_goods_promotion_url($option) {
        $result = $this->goods_promotion_url_generate($option);
        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('goods_promotion_url_generate_response', $arr)
                && array_key_exists('goods_promotion_url_list', $arr['goods_promotion_url_generate_response'])) {
                $goods_promotion_url_list = $arr['goods_promotion_url_generate_response']['goods_promotion_url_list'];
                if (!empty($goods_promotion_url_list)) {
                    return $goods_promotion_url_list[0];
                }
            }
        }

        return false;
    }

    public function goods_promotion_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::GOODS_PROMOTION_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['p_id'])) $param['p_id'] = $option['p_id'];
        if (isset($option['goods_id_list'])) $param['goods_id_list'] = '[' . $option['goods_id_list'] . ']';
        if (isset($option['generate_short_url'])) $param['generate_short_url'] = $option['generate_short_url'];
        if (isset($option['generate_we_app'])) $param['generate_we_app'] = $option['generate_we_app'];
        if (isset($option['multi_group'])) $param['multi_group'] = $option['multi_group'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
                "goods_promotion_url_generate_response":{
                    "goods_promotion_url_list":[
                        {
                            "url":"http://mobile.yangkeduo.com/duo_coupon_landing.html?goods_id=694355126&pid=230009_10075832&t=cn-DJBhPgC-8K1XtrfJMti-HyDbtS_ZHViBshmHA2ok=",
                            "short_url":"http://a.toutiaonanren.com/api/d/Z9AZpv",
                            "mobile_url":"http://mobile.yangkeduo.com/app.html?launch_url=duo_coupon_landing.html%3Fgoods_id%3D694355126%26pid%3D230009_10075832%26t%3Dcn-DJBhPgC-8K1XtrfJMti-HyDbtS_ZHViBshmHA2ok%3D",
                            "mobile_short_url":"http://a.toutiaonanren.com/api/d/Z9AZFW"
                        }
                    ]
                }
            }
        */
    }

    // 查询某天的订单，最多90天之内
    public function order_list_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::ORDER_LIST_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['p_id'])) $param['p_id'] = $option['p_id'];
        if (isset($option['page'])) $param['page'] = $option['page'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['date'])) $param['date'] = $option['date'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;

        /*
            "order_list_get_response": {
                "order_list": [{
                    "order_sn": "180409-300177171203371",
                    "goods_id": 694355126,
                    "goods_name": "100%竹浆本色抽纸28包18包8包 刘小白无荧光卫生纸3层300张/包",
                    "goods_thumbnail_url": "http://t07img.yangkeduo.com/images/2018-03-14/8cc66987827cfba6692858c760e741c3.jpeg",
                    "goods_quantity": 1,
                    "goods_price": 1290,
                    "order_amount": 990,
                    "order_create_time": 1523241828,
                    "promotion_rate": 200,
                    "promotion_amount": 198,
                    "order_status": 1,
                    "p_id": "230009_10075832"
                }],
                "total_count": 1
            }
        */
    }

    public function get_range_order_list($option) {
        $result = $this->order_list_range_get($option);
        if ($result) {
            $arr = json_decode($result, true);

            if (!empty($arr)
                && array_key_exists('order_list_get_response', $arr)
                && array_key_exists('order_list', $arr['order_list_get_response'])) {
                $order_list = $arr['order_list_get_response']['order_list'];
                if (!empty($order_list)) {
                    return $order_list;
                }
            }
        }

        return false;
    }

    // 查询某个时间段的订单，最多90天之内
    private function order_list_range_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::ORDER_LIST_RANGE_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['p_id'])) $param['p_id'] = $option['p_id'];
        if (isset($option['page'])) $param['page'] = $option['page'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['start_time'])) $param['start_time'] = $option['start_time'];
        if (isset($option['end_time'])) $param['end_time'] = $option['end_time'];
        if (isset($option['order_status'])) $param['order_status'] = $option['order_status']; //  1--已支付待成团，2--已成团，3--已确认收货，4--审核通过，5--审核失败
        if (isset($option['time_type'])) $param['time_type'] = $option['time_type']; // 0--创建时间，1--支付时间，9--最后更新时间 （默认0）

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
              "order_list_get_response": {
                "order_list": [
                  {
                    "order_sn": "180209-113067162241707",
                    "goods_id": 5411617,
                    "goods_name": "【usb酷毙灯】【美的正品】美的酷毙台灯 大学生宿舍灯管神器led护眼台灯学习寝室书桌USB阅读充电灯",
                    "goods_thumbnail_url": "http://omsproductionimg.yangkeduo.com/images/2017-09-04/4a8b4bb7fd5b85eb5234b542ca339124.jpeg",
                    "goods_quantity": 1,
                    "goods_price": 2980,
                    "order_amount": 2780,
                    "order_create_time": 1518107683,
                    "promotion_rate": 50,
                    "promotion_amount": 139,
                    "order_status": 2,
                    "verify_time": null,
                    "p_id": "10000_10001"
                  }
                ],
                "total_count": 4
              }
            }

            {
                "order_list_get_response": {
                    "order_list": [{
                        "order_sn": "180409-300177171203371",
                        "goods_id": 694355126,
                        "goods_name": "100%竹浆本色抽纸28包18包8包 刘小白无荧光卫生纸3层300张/包",
                        "goods_thumbnail_url": "http://t07img.yangkeduo.com/images/2018-03-14/8cc66987827cfba6692858c760e741c3.jpeg",
                        "goods_quantity": 1,
                        "goods_price": 1290,
                        "order_amount": 990,
                        "order_create_time": 1523241828,
                        "order_pay_time": 1523241867,
                        "promotion_rate": 200,
                        "promotion_amount": 198,
                        "order_status": 1,
                        "order_status_desc": "已成团",
                        "order_group_success_time": 1523241867,
                        "order_modify_at": 1523241877,
                        "p_id": "230009_10075832"
                    }],
                    "total_count": 1
                }
            }
        */
    }

    public function get_increment_order_list($option) {
        $result = $this->order_list_increment_get($option);
        if ($result) {
            $arr = json_decode($result, true);

            if (!empty($arr)
                && array_key_exists('order_list_get_response', $arr)
                && array_key_exists('order_list', $arr['order_list_get_response'])) {
                $order_list = $arr['order_list_get_response']['order_list'];
                if (!empty($order_list)) {
                    return $order_list;
                }
            }
        }

        return false;
    }

    // 最后更新时间段增量同步推广订单信息
    private function order_list_increment_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::ORDER_LIST_INCREMENT_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['p_id'])) $param['p_id'] = $option['p_id'];
        if (isset($option['page'])) $param['page'] = $option['page'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['start_update_time'])) $param['start_update_time'] = $option['start_update_time']; // 时间戳 最近90天内多多进宝商品订单更新时间 查询开始时间
        if (isset($option['end_update_time'])) $param['end_update_time'] = $option['end_update_time']; // 时间戳 最近90天内多多进宝商品订单更新时间 查询结束时间

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function theme_goods_search($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::THEME_GOODS_SEARCH,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['theme_id'])) $param['theme_id'] = $option['theme_id'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
        /*
            {
                "theme_list_get_response": {
                    "total": 10,
                    "goods_list": [{
                        "goods_id": 5222721,
                        "goods_name": "网红同款买2送1物理嫩白防护喷雾150ml/朴致水嫩保湿喷雾",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t01img.yangkeduo.com/images/2018-04-02/fe7ddb9488207b9b48ee085ca61eaa79.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-11-10/a33e092075983570c929e255b6ceb3d4.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 28979,
                        "min_group_price": 618,
                        "min_normal_price": 2990,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 1429, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 5000,
                        "coupon_remain_quantity": 4940,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 4.74,
                        "goods_eval_count": 133,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 10341293,
                        "goods_name": "买二送一朴致进口吻我臻致口红3.5g不掉色不易粘杯孕妇可用好口红",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t00img.yangkeduo.com/t02img/images/2018-06-10/eab33fca0e25ae8a50ab55452932abab.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-06-08/90a299fe3ea67b573542fe4a71f334a6.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 5280,
                        "min_group_price": 1800,
                        "min_normal_price": 5900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [1464, 1465, 1467, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 30000,
                        "coupon_remain_quantity": 29980,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 0,
                        "goods_eval_count": 0,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 350814,
                        "goods_name": "30片限量抢补水面膜夏季必备/朴致玻尿酸蚕丝面膜补水保湿亮肤",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t00img.yangkeduo.com/t03img/images/2018-05-31/b20661afb265a21f87a7adf44a7fec09.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-11-06/78fcf7f181d451f502bc8a1c6bb2e70b.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 126704,
                        "min_group_price": 1720,
                        "min_normal_price": 3980,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 33, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 50000,
                        "coupon_remain_quantity": 48070,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 4.87,
                        "goods_eval_count": 1069,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 188546307,
                        "goods_name": "进口植物纤维补水保湿面膜贴朴致玻尿酸补水保湿3-31片多规格可选",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t11img.yangkeduo.com/images/2018-04-01/51b354fa8f736109bd46e4e34fbd3b2c.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-11-10/6716224a934bac40d8779eb231b2a574.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 14315,
                        "min_group_price": 379,
                        "min_normal_price": 1900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 33, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9900,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 0,
                        "goods_eval_count": 0,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 531990713,
                        "goods_name": "朴致雪莲补水美白化妆品套装紧致抗皱洗面奶保湿水乳素颜霜护肤品",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t11img.yangkeduo.com/images/2018-04-02/274524fb140828741ef28cbd493d3f42.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2018-01-30/7221517721dddb46b81b802ac5bcd51c.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 57,
                        "min_group_price": 9900,
                        "min_normal_price": 39800,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 1430, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 500,
                        "coupon_discount": 500,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9990,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 0,
                        "goods_eval_count": 0,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 377709104,
                        "goods_name": "【晒后修护补水必备 免洗芦荟胶】朴致睡眠面膜补水保湿女去痘凝胶面霜自然芦荟胶正1-3盒",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t07img.yangkeduo.com/images/2018-03-16/0a3a5b2036cba84b8399d8b43f710ae6.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-12-25/7df50a2e83bd52967db00550f6787fb4.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 18741,
                        "min_group_price": 484,
                        "min_normal_price": 3900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 1429, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9540,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 4.85,
                        "goods_eval_count": 52,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 1335453,
                        "goods_name": "淡化黑眼圈细纹皱纹 朴致胶原蛋白透明质酸黄金眼膜贴5-30对任选",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t00img.yangkeduo.com/t05img/images/2018-06-05/4ed1e2828933b4b5cb3a07977cf9828f.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-09-08/146e9bdf8a4c2d27d3dd0e651ab0445b.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 98808,
                        "min_group_price": 790,
                        "min_normal_price": 990,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 71, 35, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9950,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 4.87,
                        "goods_eval_count": 114,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 1564731778,
                        "goods_name": "买2送1大罐去角质凝露150g朴致全身可用去死皮不伤肤1-3瓶任选",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t00img.yangkeduo.com/t08img/images/2018-05-31/5e8d5c9ed2d197852b608459fcbc5a6b.jpeg",
                        "goods_image_url": "http://t00img.yangkeduo.com/t11img/images/2018-05-30/469654a941a93340fd8c8f0a2b01fb64.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 2090,
                        "min_group_price": 545,
                        "min_normal_price": 2900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 70, 3724, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 1000,
                        "coupon_remain_quantity": 680,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 4.72,
                        "goods_eval_count": 620,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 28737076,
                        "goods_name": "朴致竹炭祛去黑头面膜膏60g深层清洁白头撕拉式面膜泥 多规格可选",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://omsproductionimg.yangkeduo.com/images/2018-01-11/51e122dabb6b204e31bec824cda45069.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2018-01-11/14bc492b6eabf083566dd2e962480edc.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 24561,
                        "min_group_price": 990,
                        "min_normal_price": 2900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [69, 3726, 3731, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 200,
                        "coupon_discount": 200,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9960,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 300,
                        "goods_eval_score": 0,
                        "goods_eval_count": 0,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }, {
                        "goods_id": 9851692,
                        "goods_name": "三盒装朴致零妆感气垫bb霜自然色象牙白两色遮瑕隔离提亮肤色裸妆",
                        "goods_desc": null,
                        "goods_thumbnail_url": "http://t00img.yangkeduo.com/t05img/images/2018-05-29/5c96ef09bbb1aeab9f5ed296a5dafda4.jpeg",
                        "goods_image_url": "http://omsproductionimg.yangkeduo.com/images/2017-06-07/8cff9e5693f6049d733664a58ab19d3e.jpeg",
                        "goods_gallery_urls": null,
                        "sold_quantity": 2439,
                        "min_group_price": 6900,
                        "min_normal_price": 12900,
                        "mall_name": "朴致化妆品旗舰店",
                        "category_id": 16,
                        "category_name": "美妆",
                        "opt_id": 16,
                        "opt_name": "美妆",
                        "cat_ids": [1464, 1465, 1468, 0],
                        "has_coupon": true,
                        "coupon_min_order_amount": 500,
                        "coupon_discount": 500,
                        "coupon_total_quantity": 10000,
                        "coupon_remain_quantity": 9980,
                        "coupon_start_time": 1528214400,
                        "coupon_end_time": 1530374399,
                        "promotion_rate": 500,
                        "goods_eval_score": 0,
                        "goods_eval_count": 0,
                        "cat_id": null,
                        "avg_desc": 491,
                        "avg_lgst": 480,
                        "avg_serv": 479
                    }]
                }
            }
        */
    }

    public function get_theme($option) {
        $result = $this->theme_list_get($option);

        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('theme_list_get_response', $arr)
                && array_key_exists('theme_list', $arr['theme_list_get_response'])) {
                $theme_list = $arr['theme_list_get_response']['theme_list'];
                if (!empty($theme_list)) {
                    return $theme_list;
                }
            }
        }

        return false;
    }

    public function theme_list_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::THEME_LIST_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['page'])) $param['page'] = $option['page'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;

        /*
            {
                "theme_list_get_response": {
                    "total": 63,
                    "theme_list": [{
                        "id": 692,
                        "image_url": "http://t00img.yangkeduo.com/t07img/images/2018-06-10/e8c436db139b95f9df01eb25d0bc0711.jpeg",
                        "name": "朴致品牌专场",
                        "goods_num": 10
                    }, {
                        "id": 679,
                        "image_url": "http://t00img.yangkeduo.com/t09img/images/2018-06-10/67102a03eee6f38cf474f0df777447cd.jpeg",
                        "name": "大牌疯抢 低至9.9元",
                        "goods_num": 8
                    }, {
                        "id": 657,
                        "image_url": "http://t00img.yangkeduo.com/t11img/images/2018-06-08/7019114c78d1d1aa21e74003b6747072.jpeg",
                        "name": "向大大特卖专场",
                        "goods_num": 10
                    }, {
                        "id": 608,
                        "image_url": "http://t00img.yangkeduo.com/t09img/images/2018-06-06/1a0fb39fcb588136aafc3c6b6c3d2e2c.jpeg",
                        "name": "和校服说再见",
                        "goods_num": 20
                    }, {
                        "id": 633,
                        "image_url": "http://t00img.yangkeduo.com/t11img/images/2018-06-07/2ecc8cd52323f767440ea03c9722dcc6.jpeg",
                        "name": "尽孝不贵 老爸特惠",
                        "goods_num": 18
                    }, {
                        "id": 654,
                        "image_url": "http://t00img.yangkeduo.com/t05img/images/2018-06-08/0f66be9a38d28c427bb18ef6f276d381.jpeg",
                        "name": "大牌洋货 低至9.8元",
                        "goods_num": 11
                    }, {
                        "id": 651,
                        "image_url": "http://t00img.yangkeduo.com/t05img/images/2018-06-08/9a8dd3529bba2f883a856f73679c4961.jpeg",
                        "name": "美康粉黛品牌精选",
                        "goods_num": 23
                    }, {
                        "id": 18,
                        "image_url": "http://t04img.yangkeduo.com/images/2018-05-23/ccab4086cd1d8d9b9911686153252f8e.jpeg",
                        "name": "爆款合辑",
                        "goods_num": 24
                    }, {
                        "id": 642,
                        "image_url": "http://t00img.yangkeduo.com/t02img/images/2018-06-07/ee8649d6a517b8d8f4cbf69480b1dd0a.jpeg",
                        "name": "父亲节钜惠 尽孝不贵",
                        "goods_num": 11
                    }, {
                        "id": 638,
                        "image_url": "http://t00img.yangkeduo.com/t05img/images/2018-06-07/f07d5fb60e0b9b52e087529f90cb4c8e.jpeg",
                        "name": "萌大大婴幼儿童装",
                        "goods_num": 9
                    }, {
                        "id": 636,
                        "image_url": "http://t00img.yangkeduo.com/t03img/images/2018-06-07/0dc2e357b6877448eaaa668e52c2f707.jpeg",
                        "name": "父亲节好鞋专场",
                        "goods_num": 32
                    }, {
                        "id": 534,
                        "image_url": "http://t00img.yangkeduo.com/t03img/images/2018-06-05/14d75b757db53666accdfb38576ad436.jpeg",
                        "name": "妙度潮流男装专场",
                        "goods_num": 17
                    }, {
                        "id": 602,
                        "image_url": "http://t00img.yangkeduo.com/t03img/images/2018-06-06/5a2af125de364cd4c46a9bb4f0cc8ba4.jpeg",
                        "name": "雅邦美妆春光节",
                        "goods_num": 16
                    }, {
                        "id": 601,
                        "image_url": "http://t00img.yangkeduo.com/t03img/images/2018-06-06/a1735327ef40d0400b998f5e74184e4f.jpeg",
                        "name": "澳雅婷夏季上新",
                        "goods_num": 12
                    }, {
                        "id": 599,
                        "image_url": "http://t00img.yangkeduo.com/t11img/images/2018-06-06/363699317cb86faf373e4986176af5b1.jpeg",
                        "name": "抖音爆款-我们都是社会人",
                        "goods_num": 8
                    }, {
                        "id": 247,
                        "image_url": "http://t00img.yangkeduo.com/t01img/images/2018-06-05/767d4e40cb9534e234767a4545250688.jpeg",
                        "name": "夏季驱蚊专场活动",
                        "goods_num": 15
                    }, {
                        "id": 249,
                        "image_url": "http://t00img.yangkeduo.com/t09img/images/2018-05-31/2dca1f02e7552b445c218c7743e17a1c.jpeg",
                        "name": "拉夏贝尔品牌专场",
                        "goods_num": 30
                    }, {
                        "id": 252,
                        "image_url": "http://t00img.yangkeduo.com/t02img/images/2018-05-31/fadea4413a332df6c5f81ba5eaac12ae.jpeg",
                        "name": "韩都衣舍品牌专场",
                        "goods_num": 20
                    }, {
                        "id": 241,
                        "image_url": "http://t00img.yangkeduo.com/t08img/images/2018-05-30/b05e45e46e357b6021cfaeae9780d44f.jpeg",
                        "name": "SKIMI品牌专场",
                        "goods_num": 0
                    }, {
                        "id": 43,
                        "image_url": "http://t00img.yangkeduo.com/t10img/images/2018-05-25/e1bdb7e76c9d2717fdb5d82b0ec6c255.jpeg",
                        "name": "端午节粽子节专题",
                        "goods_num": 8
                    }, {
                        "id": 28,
                        "image_url": "http://t10img.yangkeduo.com/images/2018-05-09/dd8c21f50aa90b9c6e9e9891ac7c1899.jpeg",
                        "name": "男士T恤Polo衫专场",
                        "goods_num": 16
                    }, {
                        "id": 229,
                        "image_url": "http://t00img.yangkeduo.com/t11img/images/2018-05-29/ce9c66dcbb5e1cf1f906a81db4379519.jpeg",
                        "name": "轻盈飘逸 穿出万种风情",
                        "goods_num": 16
                    }, {
                        "id": 226,
                        "image_url": "http://t00img.yangkeduo.com/t08img/images/2018-05-29/349088138d0a2d79f5977e34166d8ed9.jpeg",
                        "name": "活力仲夏梦 牛仔裤专场",
                        "goods_num": 9
                    }, {
                        "id": 47,
                        "image_url": "http://t03img.yangkeduo.com/images/2018-05-20/caac30f5cc240238c7bf0ff988ef8e38.jpeg",
                        "name": "潮流短袖 特价狂欢",
                        "goods_num": 17
                    }, {
                        "id": 48,
                        "image_url": "http://t11img.yangkeduo.com/images/2018-05-20/a13d051bd4f2c3e4b92d136a469b0477.jpeg",
                        "name": "薄款休闲裤专场",
                        "goods_num": 13
                    }, {
                        "id": 49,
                        "image_url": "http://t04img.yangkeduo.com/images/2018-05-21/e4cdb0d977ab414e2b8c80e3eb1e94af.jpeg",
                        "name": "休闲舒适妈妈装专场",
                        "goods_num": 12
                    }, {
                        "id": 472,
                        "image_url": "http://t00img.yangkeduo.com/t04img/images/2018-06-04/1afb33d9c5911951ce3b36cee0c35508.jpeg",
                        "name": "夏日凉鞋专场",
                        "goods_num": 17
                    }, {
                        "id": 248,
                        "image_url": "http://t00img.yangkeduo.com/t04img/images/2018-06-04/c3a34992556c2cff8304299ab43583c0.jpeg",
                        "name": "【水果降价啦】芒果、哈密瓜、百香果特价抢",
                        "goods_num": 9
                    }, {
                        "id": 45,
                        "image_url": "http://t04img.yangkeduo.com/images/2018-05-16/52865d231405c4fa382541a7c26e03a2.jpeg",
                        "name": "VPN品牌狂欢日",
                        "goods_num": 0
                    }, {
                        "id": 21,
                        "image_url": "http://t07img.yangkeduo.com/images/2018-05-04/4eff6cb4953665974ddad1bf5072a51e.jpeg",
                        "name": "运动健身季，秀出马甲线",
                        "goods_num": 6
                    }, {
                        "id": 255,
                        "image_url": "http://t00img.yangkeduo.com/t10img/images/2018-05-31/fcced4e610cecdc64a7dc26e5cb6e0ef.jpeg",
                        "name": "诺贝安妮特卖专场",
                        "goods_num": 0
                    }, {
                        "id": 231,
                        "image_url": "http://t00img.yangkeduo.com/t02img/images/2018-05-29/1ce7b4cad51ee0fa14e2f6d068ba0bd1.png",
                        "name": "三只松鼠大牌专场",
                        "goods_num": 2
                    }, {
                        "id": 200,
                        "image_url": "http://t07img.yangkeduo.com/images/2018-05-25/58f3fbf22619f08c66a57150c56fbc11.jpeg",
                        "name": "广西柳州螺蛳粉！！！",
                        "goods_num": 7
                    }, {
                        "id": 197,
                        "image_url": "http://t09img.yangkeduo.com/images/2018-04-23/a074132125dea9dbbe43829e3993d1b4.jpeg",
                        "name": "轻松喝上自己榨的新鲜果汁",
                        "goods_num": 11
                    }, {
                        "id": 194,
                        "image_url": "http://t00img.yangkeduo.com/t03img/images/2018-05-25/c3be15dbb5b96f4300ad3d019e84d8e5.jpeg",
                        "name": "好吃又便宜的水果大甩卖",
                        "goods_num": 5
                    }, {
                        "id": 193,
                        "image_url": "http://t10img.yangkeduo.com/images/2018-05-21/fc70990ce001976c0622f3134d29fbe4.jpeg",
                        "name": "给你的惊喜",
                        "goods_num": 11
                    }, {
                        "id": 153,
                        "image_url": "http://t03img.yangkeduo.com/images/2018-05-23/d53f66c132e5a5f62e542fa1eba872b7.jpeg",
                        "name": "陪你过六一儿童节！",
                        "goods_num": 28
                    }, {
                        "id": 152,
                        "image_url": "http://t00img.yangkeduo.com/t07img/images/2018-05-24/0adf5a674cd622c644b5b8e5ee037380.jpeg",
                        "name": "儿童节快乐",
                        "goods_num": 16
                    }, {
                        "id": 50,
                        "image_url": "http://t02img.yangkeduo.com/images/2018-05-21/4658f2cb83cbadbe03e157b8dd5f4261.jpeg",
                        "name": "夏季单品 女神必备",
                        "goods_num": 8
                    }, {
                        "id": 46,
                        "image_url": "http://t08img.yangkeduo.com/images/2018-04-03/8cddb1d16c7414cbddd8460738d8fff1.jpeg",
                        "name": "家用电器专题",
                        "goods_num": 11
                    }, {
                        "id": 44,
                        "image_url": "http://t11img.yangkeduo.com/images/2018-05-16/174a94f0bc01697588f2b67577bde61a.jpeg",
                        "name": "517吃货节专题",
                        "goods_num": 16
                    }, {
                        "id": 37,
                        "image_url": "http://t04img.yangkeduo.com/images/2018-05-14/4a833369b8ea97365a711a19e875fc10.jpeg",
                        "name": "爱丽小屋母亲节专场特惠",
                        "goods_num": 1
                    }, {
                        "id": 42,
                        "image_url": "http://omsproductionimg.yangkeduo.com/images/2017-09-04/5a64917fca3c19143301544e9eec24e5.jpeg",
                        "name": "超值袜子专场—超低价",
                        "goods_num": 13
                    }, {
                        "id": 41,
                        "image_url": "http://t08img.yangkeduo.com/images/2018-05-13/e2f7f51dfb4630d9664314df04da895a.jpeg",
                        "name": "个护美体夏日必抢",
                        "goods_num": 18
                    }, {
                        "id": 36,
                        "image_url": "http://t02img.yangkeduo.com/images/2018-05-12/9e31f876e16624772d997be451130366.jpeg",
                        "name": "母亲节彩妆专场",
                        "goods_num": 25
                    }, {
                        "id": 39,
                        "image_url": "http://t09img.yangkeduo.com/images/2018-05-13/f3d7baf388bea0f990282d7b80f9e937.jpeg",
                        "name": "彩妆大牌精选专区，高颜值好物",
                        "goods_num": 3
                    }, {
                        "id": 38,
                        "image_url": "http://t04img.yangkeduo.com/images/2018-05-13/6f74f6916505f7a38f5221f8def24264.jpeg",
                        "name": "520表白日-卡姿兰名品精选",
                        "goods_num": 7
                    }, {
                        "id": 35,
                        "image_url": "http://t08img.yangkeduo.com/images/2018-05-11/8b4ada8fbb6081f711c9c17a1b909c1f.jpeg",
                        "name": "母亲节妈妈鞋专场",
                        "goods_num": 10
                    }, {
                        "id": 34,
                        "image_url": "http://t03img.yangkeduo.com/images/2018-05-11/983168ea995e6e505aa0cdcb52ce5ed1.jpeg",
                        "name": "达芙妮专辑",
                        "goods_num": 6
                    }, {
                        "id": 33,
                        "image_url": "http://t05img.yangkeduo.com/images/2018-05-11/08423196f8af8e80dd7b364dc6163337.jpeg",
                        "name": "回力精选专辑",
                        "goods_num": 3
                    }]
                }
            }
        */
    }

    public function get_theme_prom_url($option) {
        $result = $this->theme_prom_url_generate($option);

        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('theme_promotion_url_generate_response', $arr)
                && array_key_exists('url_list', $arr['theme_promotion_url_generate_response'])) {
                $theme_promotion_url_generate_response_url_list = $arr['theme_promotion_url_generate_response']['url_list'];
                if (!empty($theme_promotion_url_generate_response_url_list)) {
                    return $theme_promotion_url_generate_response_url_list[0];
                }
            }
        }

        return false;
    }

    public function theme_prom_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::THEME_PROM_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['pid'])) $param['pid'] = $option['pid'];
        if (isset($option['theme_id_list'])) $param['theme_id_list'] = $option['theme_id_list'];
        if (isset($option['generate_short_url'])) $param['generate_short_url'] = $option['generate_short_url'];
        if (isset($option['generate_mobile'])) $param['generate_mobile'] = $option['generate_mobile'];
        if (isset($option['custom_parameters'])) $param['custom_parameters'] = $option['custom_parameters'];

        // TODO 生成小程序的推广URL

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function get_cms_prom_url($option) {
        $result = $this->cms_prom_url_generate($option);

        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('cms_promotion_url_generate_response', $arr)
                && array_key_exists('url_list', $arr['cms_promotion_url_generate_response'])) {
                $cms_promotion_url_generate_response_url_list = $arr['cms_promotion_url_generate_response']['url_list'];
                if (!empty($cms_promotion_url_generate_response_url_list)) {
                    return $cms_promotion_url_generate_response_url_list[0];
                }
            }
        }

        return false;
    }

    public function cms_prom_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::CMS_PROM_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['generate_short_url'])) $param['generate_short_url'] = $option['generate_short_url'];
        if (isset($option['p_id_list'])) $param['p_id_list'] = $option['p_id_list'];
        if (isset($option['generate_mobile'])) $param['generate_mobile'] = $option['generate_mobile'];
        if (isset($option['multi_group'])) $param['multi_group'] = $option['multi_group'];
        if (isset($option['custom_parameters'])) $param['custom_parameters'] = $option['custom_parameters'];

        // TODO 生成小程序的推广URL

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;

        /*
            {
                "cms_promotion_url_generate_response": {
                    "total": 2,
                    "url_list": [{
                        "url": "https://mobile.yangkeduo.com/duo_cms_mall.html?pid=230009_11162249&sign=3l0octdlEhs5uibK1ugZDmSknreRU0ruQehkkj2NSlI=&duoduo_type=2",
                        "short_url": "https://a.toutiaonanren.com/api/d/LSMBgA",
                        "mobile_url": "https://mobile.yangkeduo.com/app.html?launch_url=duo_cms_mall.html?pid=230009_11162249&sign=3l0octdlEhs5uibK1ugZDmSknreRU0ruQehkkj2NSlI%3D&duoduo_type=2",
                        "mobile_short_url": "https://a.toutiaonanren.com/api/d/LSMAB8",
                        "multi_group_url": null,
                        "multi_group_short_url": null,
                        "multi_group_mobile_url": null,
                        "multi_group_mobile_short_url": null,
                        "wep_app_url": null
                    }]
                }
            }
        */
    }

    public function get_rp_prom_url($option) {
        $result = $this->rp_prom_url_generate($option);

        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('rp_promotion_url_generate_response', $arr)) {
                return $arr['rp_promotion_url_generate_response'];
            }
        }

        return false;
    }

    public function rp_prom_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::RP_PROM_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['generate_short_url'])) $param['generate_short_url'] = $option['generate_short_url'];
        if (isset($option['p_id_list'])) $param['p_id_list'] = $option['p_id_list'];
        if (isset($option['custom_parameters'])) $param['custom_parameters'] = $option['custom_parameters'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function get_act_prom_url($option) {
        $result = $this->act_prom_url_generate($option);

        if ($result) {
            $arr = json_decode($result, true);
            if (!empty($arr)
                && array_key_exists('act_promotion_url_generate_response', $arr)) {
                return $arr['act_promotion_url_generate_response'];
            }
        }

        return false;
    }

    public function act_prom_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::ACT_PROM_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['url_type'])) $param['url_type'] = $option['url_type'];
        if (isset($option['generate_short_url'])) $param['generate_short_url'] = $option['generate_short_url'];
        if (isset($option['generate_mobile_short_url'])) $param['generate_mobile_short_url'] = $option['generate_mobile_short_url'];
        if (isset($option['p_id_list'])) $param['p_id_list'] = $option['p_id_list'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function app_new_bill_list_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::APP_NEW_BILL_LIST_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['pid'])) $param['pid'] = $option['pid'];
        if (isset($option['start_time'])) $param['start_time'] = $option['start_time'];
        if (isset($option['end_time'])) $param['end_time'] = $option['end_time'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['page'])) $param['page'] = $option['page'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function check_in_prom_bill_incr_get($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::CHECK_IN_PROM_BILL_INCR_GET,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['pid'])) $param['pid'] = $option['pid'];
        if (isset($option['start_time'])) $param['start_time'] = $option['start_time'];
        if (isset($option['end_time'])) $param['end_time'] = $option['end_time'];
        if (isset($option['page_size'])) $param['page_size'] = $option['page_size'];
        if (isset($option['page'])) $param['page'] = $option['page'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }

    public function check_in_prom_url_generate($option) {
        if (empty($option)) return false;
        $param = array(
            'type' => self::CHECK_IN_PROM_URL_GENERATE,
            'client_id' => $this->client_id,
            'timestamp' => TIMESTAMP,
            'data_type' => 'JSON'
        );

        if (isset($option['pid'])) $param['pid'] = $option['pid'];
        if (isset($option['custom_parameters'])) $param['custom_parameters'] = $option['custom_parameters'];

        $param['sign'] = $this->getSignature($param);

        $result = $this->post(
            self::API_ROOT,
            $param
        );

        return $result;
    }
}